#ifndef WWFRONTEND_BIZUIMSGTRANSFER_H
#define WWFRONTEND_BIZUIMSGTRANSFER_H

#include "../Core/msgbus_handlerbase.hpp"
#include "../Core/msgbus_interface.h"
#include <boost/noncopyable.hpp>
#include <string>
#include <deque>
#include <map>

using namespace NetMsgBus;

class BizUIMsgTransfer;
DECLARE_SP_PTR(BizUIMsgTransfer);

class BizUIMsgTransfer : public MsgHandler<BizUIMsgTransfer>, public boost::noncopyable
{
public:
    friend class NetMsgBus::MsgHandlerMgr;
    static std::string ClassName()
    {
        return "BizUIMsgTransfer";
    }
    static BizUIMsgTransferPtr Instance();
    void InitMsgHandler();
    ~BizUIMsgTransfer();
    void Init(const std::string& immain_name);
    void StartIMReceiver();
    void WaitIMReceiverQuit();
    void StopIMReceiver();
private:
    BizUIMsgTransfer(){}
    void SendMsgToIMReceiverOrCached(const std::string& msgid, MsgBusParam imparam);
    bool ConvertMsgId(const std::string& srcmsgid, std::string& destmsgid);
    void WaitUIQuitThreadFunc(pid_t wpid);

    bool OnUpdateGroupFinished(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnUpdateContactFinished(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnBuildingU2GRelationFinished(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnLogout(const std::string& msgid, MsgBusParam& param, bool& is_continue);

    bool OnForwardBizMsgToUIFrontend(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnForwardUIMsgToBiz(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    //bool OnAddedContact(const std::string& msgid, MsgBusParam& param, bool& is_continue);

    bool OnImReceiverReady(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnImReceiverClosed(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnImReceiverError(const std::string& msgid, MsgBusParam& param, bool& is_continue);

    //bool OnFrontendSendMsg(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnDoLogin(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnDoReLogin(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnDoLogout(const std::string& msgid, MsgBusParam& param, bool& is_continue);

    // 由于消息会发给进程外的消息接收者,为了防止不同用户的消息错穿,这里使用一个标识符指示与当前关联的消息接收者
    //
    std::string m_imreceiver_uniquename;
    bool  m_imreceiver_ready;
    pid_t m_imreceiver_pid;
    std::string m_immain_name;
    typedef std::map<std::string, std::deque<MsgBusParam> > CachedMsgContainerT;
    // the messages waiting to be send to imreceiver when it ready.
    CachedMsgContainerT m_cached_msgs;
    bool m_isgroup2contacts_ready;
    std::map<std::string, std::string> m_biz2ui_msg_map;
    std::map<std::string, std::string> m_ui2biz_msg_map;
};
#endif
